﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Koonsoft.HYCRM.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    /// <summary>
    /// 业务委托访问数据层
    /// </summary>
    public class DelegateDAL
    {
        private Database database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        /// <summary>
        /// 根据业务编号获取业务名称
        /// </summary>
        /// <param name="delegateType"></param>
        /// <returns></returns>
        public string GetDelegateNameByDelegateType(int delegateType)
        {
            const string sql = "select DelegateName from Delegate where DelegateType=@DelegateType";

            using (var cmd = database.GetSqlStringCommand(sql))
            {
                database.AddInParameter(cmd, "@DelegateType", DbType.Int32, delegateType);

                return database.ExecuteScalar(cmd).ToString();
            }
        }

        /// <summary>
        /// 根据权限获取业务类型
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="deliverType"></param>
        /// <returns></returns>
        public List<DelegateInfo> GetDelegatesByRoleID(int roleID, int deliverType)
        {
            var delegates = new List<DelegateInfo>();

            const string procedure = "Delegate_GetDelegatesByRoleID";

            using (var cmd = database.GetStoredProcCommand(procedure))
            {
                database.AddInParameter(cmd, "@deliverType", DbType.Int32, deliverType);
                database.AddInParameter(cmd, "@roleID", DbType.Int32, roleID);

                var rd = (RefCountingDataReader)database.ExecuteReader(cmd);
                var sqlrd = (SqlDataReader)rd.InnerReader;

                while (sqlrd.Read())
                {
                    delegates.Add(new DelegateInfo
                                      {
                                          DelegateName = sqlrd["DelegateName"].ToString(),
                                          DelegateType = Convert.ToInt32(sqlrd["DelegateType"])
                                      });
                }
            }

            return delegates;
        }


        /// <summary>
        /// 获取所有业务类型
        /// </summary>
        /// <returns></returns>
        public List<DelegateInfo> GetDelegates()
        {
            var delegates = new List<DelegateInfo>();

            const string sql = "select * from delegate order by delegatename";

            using (var cmd = database.GetSqlStringCommand(sql))
            {
                var rd = (RefCountingDataReader)database.ExecuteReader(cmd);

                var sqlrd = (SqlDataReader)rd.InnerReader;

                while (sqlrd.Read())
                {
                    var delegateItem = new DelegateInfo
                                           {
                                               DelegateType = Convert.ToInt32(sqlrd["DelegateType"]),
                                               DelegateName = sqlrd["DelegateName"].ToString()
                                           };
                    delegates.Add(delegateItem);
                }
            }

            return delegates;
        }
    }
}
